package com.etime.shycourse.controller.coupons;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.pojo.Couponactivetype;
import com.etime.shycourse.pojo.Couponactivity;
import com.etime.shycourse.pojo.Couponactivityref;
import com.etime.shycourse.services.coupons.interfaces.CouponactivetypeServices;
import com.etime.shycourse.services.coupons.interfaces.CouponactivityServices;
import com.etime.shycourse.services.coupons.interfaces.CouponactivityrefServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author wb
 * @since 2022-03-15
 */
@Api(tags = "后台-活动管理-活动数据接口")
@RestController
@RequestMapping("/sys/couponactivity")
public class CouponactivityController {
    @Autowired
    private CouponactivetypeServices couponactivetypeServices;
    @Autowired
    private CouponactivityrefServices couponactivityrefServices;
    @Autowired
    private CouponactivityServices services;

    @ApiOperation("根据活动类型ID，查询活动类型信息")
    @GetMapping("/type/get/{typeId}")
    public PojoMessage<Couponactivetype> selectTypeById(@PathVariable Integer typeId) {
        PojoMessage<Couponactivetype> msg = new PojoMessage<> ();
        Couponactivetype data = couponactivetypeServices.selectById (typeId);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有活动类型");
        }
        return msg;
    }


    @ApiOperation("查询活动可用的类型信息")
    @GetMapping("/type/all")
    public ListMessage<Couponactivetype> selectAll() {
        ListMessage<Couponactivetype> msg = new ListMessage<> ();
        List<Couponactivetype> data = couponactivetypeServices.selectAll ();
        if (data.size () > 0) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有可用的活动类型");
        }
        return msg;
    }

    @ApiOperation("查询活动与优惠券存在对应关系")
    @GetMapping("/get/tivity/{activityId}")
    public ListMessage<Couponactivityref> selectActivityRefById(@PathVariable Integer activityId) {
        ListMessage<Couponactivityref> msg = new ListMessage<>();
        List<Couponactivityref> data = couponactivityrefServices.selectActivityRefById(activityId);
        if (data != null) {
            msg.setCode(1);
            msg.setDatalist(data);
        } else {
            msg.setCode(0);
            msg.setMsg("该活动与优惠券没有对应关系");
        }
        return msg;
    }

    @ApiOperation("创建活动对应的优惠劵。返回int 成功 =1 失败=0 ")
    @PostMapping("/tivity/inset")
    public BaseMessage insertCouponActivity(@RequestBody List<Couponactivityref> couponactivity, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        int i = couponactivityrefServices.insertCouponActivity (couponactivity);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("修改成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改失敗");
        }
        return msg;
    }

    @ApiOperation("创建活动。返回int 成功 =1 失败=0 ")
    @PostMapping("/inset")
    public BaseMessage insertActivity(Couponactivity couponactivity, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        int i = services.insertActivity (couponactivity, token);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("创建成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("创建失敗");
        }
        return msg;
    }

    @ApiOperation("修改活动。返回int 成功 =1 失败=0 ")
    @PostMapping("/up")
    public BaseMessage updateActivityById(Couponactivity couponactivity, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        int i = services.updateActivityById (couponactivity, token);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("修改成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改失敗");
        }
        return msg;
    }

    @ApiOperation("改变活动状态。返回int 成功 =1 失败=0 ")
    @PostMapping("/up/release")
    public BaseMessage updateActivityByIds(@RequestBody Couponactivity couponactivity) {
        BaseMessage msg = new BaseMessage ();
        int i = services.updateActivityById (couponactivity);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("发布成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("发布失敗");
        }
        return msg;
    }

    @ApiOperation("查询所有的活动 分頁")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "没有加中文的可不填", value = "注意中文注解"),
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行"),
            // 复用 活动的条件搜索
            @ApiImplicitParam(name = "activestate", value = "根据活动状态"),
            @ApiImplicitParam(name = "activetitle", value = "根据活动标题"),
            @ApiImplicitParam(name = "activeallowdforward", value = "活动是否允许转发"),
            @ApiImplicitParam(name = "activeway", value = "自动发放or手动领取")
    })
    @GetMapping("/page")
    public PageMessage<Couponactivity> selectActivityPage(Couponactivity couponactivity, Long pagenum, Long pagesize) {
        PageMessage<Couponactivity> msg = new PageMessage<> ();
        Page<Couponactivity> data = services.selectActivityPage (couponactivity, pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("根据活动ID，查询活动信息")
    @GetMapping("/get/{Id}")
    public PojoMessage<Couponactivity> selectById(@PathVariable Integer Id) {
        PojoMessage<Couponactivity> msg = new PojoMessage<> ();
        Couponactivity data = services.selectById (Id);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有活动类型");
        }
        return msg;
    }
}

